package yuyicman.display 
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import yuyicman.core.IDestroy;
	import yuyicman.debug.Debugger;
	import yuyicman.events.DataProviderEvent;
	/**
	 * ...
	 * @author Yuyic.
	 */
	public class ButtonsGroup extends EventDispatcher implements IDestroy
	{
		
		private var _buttons:Array;
		private var _currentButton:IButton;
		
		
		public function ButtonsGroup() 
		{
			_buttons = new Array();
		}
		
		public function addButton( button:IButton ):IButton {
			
			_buttons.push( button );
			button.addEventListener( "select", _buttonselecthandler );
			return button;
			
		}
		
		public function setSelect( button:IButton ):Boolean {
			
			for each( var btn:IButton in _buttons ) {
				if ( btn == button ) {
					
					if ( _currentButton ) {
						_currentButton.select = false;
					}
					_currentButton = button;
					_currentButton.select = true;
					return true;
				}
			}
			return false;
			
		}
		
		private function _buttonselecthandler( e:Event ):void {
			
			if ( _currentButton == e.target ){
				return;
			}
			
			if ( _currentButton ) {
				_currentButton.select = false;
			}
			_currentButton = e.target as IButton;
			
			dispatchEvent( new DataProviderEvent("change",e.target) );
			
		}
		
		public function destroy():void {
			
			for each( var button:IButton in _buttons ) {
				button.removeEventListener( "select", _buttonselecthandler );
			}
			_buttons = null;
			_currentButton = null;
			
		}
		
	}

}